﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Li.DingTalk.Model.Contacts
{
    /// <summary>
    /// 微应用管理处理类
    /// </summary>
    public class microappHD
    {
        /// <summary>
        /// 创建微应用
        /// </summary>
        /// <param name="access_token"></param>
        /// <param name="cms">创建微应用结构体</param>
        /// <returns></returns>
        public static createmicroapplication Createmicroapplication(string access_token, createmicroapplicationstructural cms)
        {
            var path = string.Format("https://oapi.dingtalk.com/microapp/create?access_token={0}", access_token);
            var StrJson = Newtonsoft.Json.JsonConvert.SerializeObject(cms);
            var create = RequestHelper.Post(path, StrJson);
            return Newtonsoft.Json.JsonConvert.DeserializeObject<createmicroapplication>(create);
        }

        /// <summary>
        /// 更新微应用
        /// </summary>
        /// <param name="access_token"></param>
        /// <param name="ums">更新微应用结构体</param>
        /// <returns></returns>
        public static updatemicroapplication Updatemicroapplication(string access_token, updatemicroapplicationstructural ums)
        {
            var path = string.Format("https://oapi.dingtalk.com/microapp/update?access_token={0}", access_token);
            var StrJson = Newtonsoft.Json.JsonConvert.SerializeObject(ums);
            var update = RequestHelper.Post(path, StrJson);
            return Newtonsoft.Json.JsonConvert.DeserializeObject<updatemicroapplication>(update);
        }

        /// <summary>
        /// 删除微应用
        /// </summary>
        /// <param name="access_token"></param>
        /// <param name="dms">删除微应用结构体</param>
        /// <returns></returns>
        public static ResultPackage Deletemicroapplication(string access_token, deletemicroapplicationstructural dms)
        {
            var path = string.Format("https://oapi.dingtalk.com/microapp/delete?access_token={0}", access_token);
            var StrJson = Newtonsoft.Json.JsonConvert.SerializeObject(dms);
            var delete = RequestHelper.Post(path, StrJson);
            return Newtonsoft.Json.JsonConvert.DeserializeObject<ResultPackage>(delete);
        }

        /// <summary>
        /// 列出微应用
        /// </summary>
        /// <param name="access_token"></param>
        /// <returns></returns>
        public static microapplicationlist Microapplicationlist(string access_token, microapplicationliststructural mls)
        {
            var path = string.Format("https://oapi.dingtalk.com/microapp/list?access_token={0}", access_token);
            var StrJson = Newtonsoft.Json.JsonConvert.SerializeObject(mls);
            var delete = RequestHelper.Post(path, StrJson);
            return Newtonsoft.Json.JsonConvert.DeserializeObject<microapplicationlist>(delete);
        }

        /// <summary>
        /// 获取企业设置的微应用可见范围
        /// </summary>
        /// <param name="access_token"></param>
        /// <param name="oms">获取企业设置的微应用可见范围的结构体</param>
        /// <returns></returns>
        public static obtainmicroapplicationvisible Obtainmicroapplicationvisible(string access_token, obtainmicroapplicationvisiblestructural oms)
        {
            var path = string.Format("https://oapi.dingtalk.com/microapp/visible_scopes?access_token={0}", access_token);
            var StrJson = Newtonsoft.Json.JsonConvert.SerializeObject(oms);
            var visible = RequestHelper.Post(path, StrJson);
            return Newtonsoft.Json.JsonConvert.DeserializeObject<obtainmicroapplicationvisible>(visible);
        }

        /// <summary>
        /// 设置微应用的可见范围
        /// </summary>
        /// <param name="access_token"></param>
        /// <param name="sms">设置微应用的可见范围的结构体</param>
        /// <returns></returns>
        public static ResultPackage Setmicroapplicationvisible(string access_token, setmicroapplicationvisiblestructural sms)
        {
            var path = string.Format("https://oapi.dingtalk.com/microapp/set_visible_scopes?access_token={0}", access_token);
            var StrJson = Newtonsoft.Json.JsonConvert.SerializeObject(sms);
            var setvisible = RequestHelper.Post(path, StrJson);
            return Newtonsoft.Json.JsonConvert.DeserializeObject<ResultPackage>(setvisible);
        }

        /// <summary>
        /// 获取指定微应用下指定用户绑定的全部规则
        /// </summary>
        /// <param name="access_token"></param>
        /// <param name="gar">获取指定微应用下指定用户绑定的全部规则的结构体</param>
        /// <returns></returns>
        public static getallrules Getallrules(string access_token, getallrulesstructural gar)
        {
            var path = string.Format("https://oapi.dingtalk.com/microapp/rule/get_rule_list?access_token={0}", access_token);
            var StrJson = Newtonsoft.Json.JsonConvert.SerializeObject(gar);
            var getallrules = RequestHelper.Post(path, StrJson);
            return Newtonsoft.Json.JsonConvert.DeserializeObject<getallrules>(getallrules);
        }

        /// <summary>
        /// 获取规则绑定的用户数
        /// </summary>
        /// <param name="access_token"></param>
        /// <param name="gar">获取规则绑定的用户数的结构体</param>
        /// <returns></returns>
        public static getrulesusercount Getrulesusercount(string access_token, getrulesusercountstructural gcs)
        {
            var path = string.Format("https://oapi.dingtalk.com/microapp/rule/get_user_total?access_token={0}", access_token);
            var StrJson = Newtonsoft.Json.JsonConvert.SerializeObject(gcs);
            var getallrules = RequestHelper.Post(path, StrJson);
            return Newtonsoft.Json.JsonConvert.DeserializeObject<getrulesusercount>(getallrules);
        }

        /// <summary>
        /// 删除规则
        /// </summary>
        /// <param name="access_token"></param>
        /// <param name="gar">获取规则绑定的用户数的结构体</param>
        /// <returns></returns>
        public static ResultPackage Deleterules(string access_token, deleterulesstructural drs)
        {
            var path = string.Format("https://oapi.dingtalk.com/microapp/rule/delete?access_token={0}", access_token);
            var StrJson = Newtonsoft.Json.JsonConvert.SerializeObject(drs);
            var deleterules = RequestHelper.Post(path, StrJson);
            return Newtonsoft.Json.JsonConvert.DeserializeObject<ResultPackage>(deleterules);
        }

    }

    #region 微应用信息
    /// <summary>
    /// 创建微应用结构体
    /// </summary>
    public class createmicroapplicationstructural
    {
        /// <summary>
        /// 微应用的图标。需要调用上传接口将图标上传到钉钉服务器后获取到的mediaId
        /// </summary>
        public string appIcon
        {
            get;
            set;
        }
        /// <summary>
        /// 微应用的名称。长度限制为1~10个字符
        /// </summary>
        public string appName
        {
            get;
            set;
        }
        /// <summary>
        /// 微应用的描述。长度限制为1~20个字符
        /// </summary>
        public string appDesc
        {
            get;
            set;
        }
        /// <summary>
        /// 微应用的移动端主页，必须以http开头或https开头
        /// </summary>
        public string homepageUrl
        {
            get;
            set;
        }
        /// <summary>
        /// 微应用的PC端主页，必须以http开头或https开头，如果不为空则必须与homepageUrl的域名一致
        /// </summary>
        public string pcHomepageUrl
        {
            get;
            set;
        }
        /// <summary>
        /// 微应用的OA后台管理主页，必须以http开头或https开头。微应用后台管理员免登开发
        /// </summary>
        public string ompLink
        {
            get;
            set;
        }
    }

    /// <summary>
    /// 创建微应用
    /// </summary>
    public class createmicroapplication : ResultPackage
    {
        /// <summary>
        /// 创建的微应用id
        /// </summary>
        public string agentId
        {
            get;
            set;
        }
    }

    /// <summary>
    /// 更新微应用结构体
    /// </summary>
    public class updatemicroapplicationstructural
    {
        /// <summary>
        /// 微应用的图标。需要调用上传接口将图标上传到钉钉服务器后获取到的mediaId
        /// </summary>
        public string appIcon
        {
            get;
            set;
        }
        /// <summary>
        /// 微应用的名称。长度限制为1~10个字符
        /// </summary>
        public string appName
        {
            get;
            set;
        }
        /// <summary>
        /// 微应用的描述。长度限制为1~20个字符
        /// </summary>
        public string appDesc
        {
            get;
            set;
        }
        /// <summary>
        /// 微应用的移动端主页，必须以http开头或https开头
        /// </summary>
        public string homepageUrl
        {
            get;
            set;
        }
        /// <summary>
        /// 微应用的PC端主页，必须以http开头或https开头，如果不为空则必须与homepageUrl的域名一致
        /// </summary>
        public string pcHomepageUrl
        {
            get;
            set;
        }
        /// <summary>
        /// 微应用的OA后台管理主页，必须以http开头或https开头。微应用后台管理员免登开发
        /// </summary>
        public string ompLink
        {
            get;
            set;
        }
        /// <summary>
        /// 微应用的id
        /// </summary>
        public int agentId
        {
            get;
            set;
        }
    }

    /// <summary>
    /// 更新微应用
    /// </summary>
    public class updatemicroapplication : ResultPackage
    {
        /// <summary>
        /// 创建的微应用id
        /// </summary>
        public string agentId
        {
            get;
            set;
        }
    }

    /// <summary>
    /// 删除微应用结构体
    /// </summary>
    public class deletemicroapplicationstructural
    {
        /// <summary>
        /// 微应用的id。企业只能删除自建微应用
        /// </summary>
        public int agentId
        {
            get;
            set;
        }
    }

    /// <summary>
    /// 列出微应用的结构体
    /// </summary>
    public class microapplicationliststructural
    {
        /// <summary>
        /// 微应用的id
        /// </summary>
        public int agentId
        {
            get;
            set;
        }
    }

    /// <summary>
    /// 列出微应用信息
    /// </summary>
    public class microapplicationlist : ResultPackage
    {
        /// <summary>
        /// 微应用信息
        /// </summary>
        public application[] appList
        {
            get;
            set;
        }
    }

    /// <summary>
    /// 列出微应用信息
    /// </summary>
    public class application
    {
        /// <summary>
        /// 微应用图标
        /// </summary>
        public string appIcon
        {
            get;
            set;
        }

        /// <summary>
        /// 微应用id
        /// </summary>
        public int agentId
        {
            get;
            set;
        }

        /// <summary>
        /// 微应用描述
        /// </summary>
        public string appDesc
        {
            get;
            set;
        }

        /// <summary>
        /// 表示是否是自建微应用
        /// </summary>
        public bool isSelf
        {
            get;
            set;
        }

        /// <summary>
        /// 微应用名称
        /// </summary>
        public string name
        {
            get;
            set;
        }

        /// <summary>
        /// 微应用的移动端主页
        /// </summary>
        public string homepageUrl
        {
            get;
            set;
        }

        /// <summary>
        /// 微应用的pc端主页
        /// </summary>
        public string pcHomepageUrl
        {
            get;
            set;
        }

        /// <summary>
        /// 微应用状态，1表示启用，0表示停用
        /// </summary>
        public int appStatus
        {
            get;
            set;
        }

        /// <summary>
        /// 微应用的OA后台管理主页
        /// </summary>
        public string ompLink
        {
            get;
            set;
        }
    }

    /// <summary>
    /// 获取企业设置的微应用可见范围的结构体
    /// </summary>
    public class obtainmicroapplicationvisiblestructural
    {
        /// <summary>
        /// 需要查询的微应用agentId
        /// </summary>
        public long agentId
        {
            get;
            set;
        }
    }

    /// <summary>
    /// 获取企业设置的微应用可见范围信息
    /// </summary>
    public class obtainmicroapplicationvisible : ResultPackage
    {
        /// <summary>
        /// 是否仅限管理员可见
        /// </summary>
        public bool isHidden
        {
            get;
            set;
        }

        /// <summary>
        /// 微应用的可见部门id列表，格式为JSON数组
        /// </summary>
        public long[] deptVisibleScopes
        {
            get;
            set;
        }

        /// <summary>
        /// 微应用的可见用户id列表，格式为JSON数组
        /// </summary>
        public string[] userVisibleScopes
        {
            get;
            set;
        }
    }

    /// <summary>
    /// 设置微应用的可见范围的结构体
    /// </summary>
    public class setmicroapplicationvisiblestructural
    {
        /// <summary>
        /// 微应用agentId
        /// </summary>
        public long agentId
        {
            get;
            set;
        }

        /// <summary>
        /// 是否仅限管理员可见，true代表仅限管理员可见
        /// </summary>
        public bool isHidden
        {
            get;
            set;
        }

        /// <summary>
        /// 设置可见的部门id列表，格式为JSON数组
        /// </summary>
        public long[] deptVisibleScopes
        {
            get;
            set;
        }

        /// <summary>
        /// 设置可见的员工id列表，格式为JSON数组
        /// </summary>
        public string[] userVisibleScopes
        {
            get;
            set;
        }
    }

    /// <summary>
    /// 获取指定微应用下指定用户绑定的全部规则的结构体
    /// </summary>
    public class getallrulesstructural
    {
        /// <summary>
        /// 用户在企业内的唯一标识
        /// </summary>
        public string userid
        {
            get;
            set;
        }

        /// <summary>
        /// 微应用在企业内的id
        /// </summary>
        public string agentId
        {
            get;
            set;
        }
    }

    /// <summary>
    /// 获取指定微应用下指定用户绑定的全部规则
    /// </summary>
    public class getallrules : ResultPackage
    {
        /// <summary>
        /// 用户绑定的规则id列表
        /// </summary>
        public int[] ruleIdList
        {
            get;
            set;
        }
    }

    /// <summary>
    /// 获取规则绑定的用户数的结构体
    /// </summary>
    public class getrulesusercountstructural
    {
        /// <summary>
        /// 规则所属的微应用agentId
        /// </summary>
        public long agentId
        {
            get;
            set;
        }

        /// <summary>
        /// 需要查询的规则id集合
        /// </summary>
        public string[] ruleIdList
        {
            get;
            set;
        }
    }

    /// <summary>
    /// 获取规则绑定的用户数
    /// </summary>
    public class getrulesusercount : ResultPackage
    {
        /// <summary>
        /// 返回入参中各个规则id对应的用户数
        /// </summary>
        public getrulesusercountlist[] result
        {
            get;
            set;
        }
    }

    /// <summary>
    /// 返回入参中各个规则id对应的用户数的信息
    /// </summary>
    public class getrulesusercountlist
    {
        /// <summary>
        /// 规则id
        /// </summary>
        public long agentId
        {
            get;
            set;
        }
        /// <summary>
        /// 用户数
        /// </summary>
        public long ruleIdList
        {
            get;
            set;
        }
    }

    /// <summary>
    /// 删除规则
    /// </summary>
    public class deleterulesstructural
    {
        /// <summary>
        /// 规则所属的微应用agentId
        /// </summary>
        public long agentId
        {
            get;
            set;
        }

        /// <summary>
        /// 被删除的规则id
        /// </summary>
        public long ruleId
        {
            get;
            set;
        }
    }

    #endregion
}
